<template>
    <div class="p-18 bg-white">

        <!--<div class="text-primary">点击大队获取大队所属高速</div>-->
        <!--<a-radio-group v-model="deptId" class="d-block" @change="depChange">-->
            <!--<a-radio :value="item.value" class="d-block mt-10" v-for="item in deps" :key="item.value">-->
                <!--{{item.label}}-->
            <!--</a-radio>-->
        <!--</a-radio-group>-->


        <div class="mt-24 pt-18 border-top">
            <div class="text-primary">高速</div>
            <div class="mt-18 d-flex" v-for="(item,i) in ways" :key="i">

                <div style="width: 200px;">{{item.name}}</div>
                <div>
                    <a-button class="mr-10" type="primary" size="small" @click="getWayCoordinate(item)">
                        获取高速---------公里桩坐标
                    </a-button>
                    <a-button type="primary" size="small" @click="getWayCoordinate2(item)">
                        获取高速---------坐标
                    </a-button>
                </div>


            </div>


        </div>


        <!--<div class="mt-24 pt-18 border-top" style="width: 600px">-->
            <!--<div class="my-14">添加大队</div>-->
            <!--<div>-->
                <!--<span>名字:</span>-->
                <!--<a-input v-model="dep.label"/>-->
            <!--</div>-->
            <!--<div class="mt-14">-->
                <!--<span>id:</span>-->
                <!--<a-input v-model="dep.value"/>-->
            <!--</div>-->
            <!--<a-button type="primary" size="small" @click="deps.push(dep)" class="mt-14">-->
                <!--添加-->
            <!--</a-button>-->
        <!--</div>-->


    </div>
</template>

<script>
    import {getexpresswayList, pileToCrd,getMotorWay} from "../../common/api";
    import {downloadFile} from "../../utils/js-utils";
    import {formatTake} from "../../utils/sys_utils";

    export default {
        name: "getWayCoordinate",
        data() {
            return {
                deptId: null,
                deps: [
                    {value: '370296000010', label: '同三高速大队'},
                    {value: '370295000010', label: '青平高速大队'},
                    {value: '370298000010', label: '胶州湾高速大队'},
                    {value: '370297000010', label: '潍莱高速大队'},
                ],
                dep: {},
                ways: [],


                wayTakes: [],
                checkWay: {},

                wayCrd: [],

                step: 100,
            }
        },
        methods: {
            depChange() {
                if (!this.deptId) {
                    this.$message.error("没有检测到大队id");
                    return;
                }
                this.$message.loading({content: '大队查询中...', key: 'loading1', duration: 0});


            },

            getWayCoordinate(item) {
                this.$message.loading({content: '数据一个接一个请求中...', key: 'loadingKey',duration:0});

                this.wayTakes = [];
                this.checkWay = item;

                var startTake = item.startTake * 1;
                var endTake = item.endTake * 1;

                this.getPileToCrd(item.id, startTake, (take) => {
                    return take <= endTake;
                })
            },
            getWayCoordinate2(item) {
                this.$message.loading({content: '数据一个接一个请求中...', key: 'loadingKey',duration:0});

                this.wayCrd = [];
                this.checkWay = item;

                var startTake = item.startTake * 1;
                var endTake = item.endTake * 1;

                this.getRoayToCrd(item.id, startTake, 0, (take) => {
                    console.log(endTake,take)
                    return take <= endTake;
                })
            },


            getPileToCrd(dldm, take, testFun) {

                var newTake = formatTake(take)+ "000";
                var wayInfo = {deptid: this.deptId, motorwayid: this.checkWay.id, take: take};//构建公里桩信息

                pileToCrd(newTake, dldm).then((res) => { // 请求公里桩坐标点
                    if (res.result === "true" || res.result === true) { //请求成功
                        wayInfo.coordinate = res.crd;  //更新坐标点
                        this.wayTakes.push(wayInfo);   //加入公路坐标点
                        if (testFun(take)) {  //还在公里桩范围内继续请求
                            this.getPileToCrd(dldm, take + 1, testFun)
                        } else {
                            this.$message.success({content: '数据请求成功...', key: 'loadingKey'});
                            downloadFile(JSON.stringify(this.wayTakes), this.checkWay.name + "公里桩.txt")
                            this.wayTakes = [];

                        }
                    } else {
                        wayInfo.coordinate = "失败";
                        this.wayTakes.push(wayInfo);
                        if (testFun(take)) {  //还在公里桩范围内继续请求
                            this.getPileToCrd(dldm, take + 1, testFun)
                        } else {
                            this.$message.success({content: '数据请求成功...', key: 'loadingKey'});
                            downloadFile(JSON.stringify(this.wayTakes), this.checkWay.name + "公里桩.txt");
                            this.wayTakes = [];
                        }
                    }

                });
            },
            getRoayToCrd(dldm, take, offset, testFun) {
                var newTake = formatTake(take)+this.formatOffset(offset);

                pileToCrd(newTake, dldm).then((res) => { // 请求公里桩坐标点
                    if (res.result === "true" || res.result === true) { //请求成功
                        this.wayCrd.push(res.crd);  //更新坐标点
                        if (testFun(take)) {  //还在公里桩范围内继续请求
                            if(offset<(1000-this.step)){
                                this.getRoayToCrd(dldm, take,(offset+this.step), testFun)
                            }else{
                                this.getRoayToCrd(dldm, take+1,0, testFun)
                            }
                        } else {
                            this.$message.success({content: '数据请求成功...', key: 'loadingKey'});
                            downloadFile(JSON.stringify(this.wayCrd.join(',')), this.checkWay.name + "坐标.txt")
                            this.wayCrd = [];

                        }
                    } else {
                        this.wayCrd.push("失败");
                        if (testFun(take)) {  //还在公里桩范围内继续请求
                            if(offset<(1000-this.step)){
                                this.getRoayToCrd(dldm, take,(offset+this.step), testFun)
                            }else{
                                this.getRoayToCrd(dldm, take+1,0, testFun)
                            }
                        } else {
                            this.$message.success({content: '数据请求成功...', key: 'loadingKey'});
                            downloadFile(JSON.stringify(this.wayCrd.join(',')), this.checkWay.name + "坐标.txt");
                            this.wayCrd = [];
                        }
                    }

                });
            },

            formatOffset(offset){
                var newOffse;
                if (offset < 10) { //  补全公里桩偏移
                    newOffse = "00" + String(offset);
                } else if (offset < 100) {
                    newOffse = "0" + String(offset);
                } else {
                    newOffse = String(offset);
                }
                return newOffse;

            }
        },
        mounted(){
            getMotorWay().then(res => {
                if (res.isSuccess) {
                    this.$message.success({content: '大队查询成功!', key: 'loading1'});
                    this.ways = res.data;

                } else {
                    this.$message.error({content: '大队查询失败!', key: 'loading1'});

                }

            })
        }
    }
</script>

<style scoped>

</style>
